/**
 * 作为测试文件，包含所有请求，并获取参数
 * query
 * params
 * body
 */

const { router, libQuery } = require('../lib/route')

// 不能打开，为了让前端报错
// const { libPathQuery } = require('../lib/route')
// libPathQuery('api', '/');

libQuery((req, res, next, method) => {
  const contype = req.headers['content-type'];
  // get
  const query = req.query
  console.log('query=', query, ' content-type=', contype)
  // post/put/delete/option
  console.log('body=', req.body)

  const cookies = req.cookies
  console.log('cookies=', cookies ? JSON.stringify(cookies) : 'unknow')
  console.log('req.headers', req.headers)
  console.log('params=', req.params) // 不会有值
  // console.log('req=', req)
  // console.log('res=', res)
  // if (!contype || contype.indexOf('application/json') !== 0) {
  //   res.send(400)
  // }
  // 参数都是字符串，注意转换问题
  const data = { title: 'Express-home', method, 'content-type': contype }
  res.send(JSON.stringify(data));
}, '/home')

libQuery((req, res, next, method) => {
  const contype = req.headers['content-type'];
  // get
  const query = req.query
  console.log('22 query=', query, ' content-type=', contype)
  // post/put/delete/option
  console.log('22 body=', req.body)

  // https://www.cnblogs.com/houfee/p/10370154.html
  // 当地址   url/:xx/:xx/:xx 才会使用 params
  console.log('22 params=', req.params)

  const cookies = req.cookies
  console.log('22 cookies=', cookies ? JSON.stringify(cookies) : 'unknow')
  console.log('22 req.headers', req.headers)

  // console.log('req=', req)
  // console.log('res=', res)
  // if (!contype || contype.indexOf('application/json') !== 0) {
  //   res.send(400)
  // }
  // 参数都是字符串，注意转换问题
  const data = { title: '222-Express-home', method, '222-content-type': contype }
  res.send(JSON.stringify(data));
}, '/home/:id/:name')

module.exports = router;
